<html>
<head>
<style>
  .ace_editor {
    border: 0;
    width: auto;
    height: 100%;
    margin: 0;
    top:0;
    right:0;
    bottom:0;
    left:0;
    z-index: 10000;
    touch-action: none;
  }

  body {
    padding: 0px;
    margin: 0px;
    overflow: hidden;
    -ms-overflow-style: scrollbar;
  }
</style>
</head>
<body>
<textarea id="source">package com.sencha.gxt.explorer.client.grid;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.cell.client.DateCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.core.client.IdentityValueProvider;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.data.client.loader.RpcProxy;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.data.shared.loader.BeforeLoadEvent;
import com.sencha.gxt.data.shared.loader.BeforeLoadEvent.BeforeLoadHandler;
import com.sencha.gxt.data.shared.loader.LoadResultListStoreBinding;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.examples.resources.client.ExampleService;
import com.sencha.gxt.examples.resources.client.ExampleServiceAsync;
import com.sencha.gxt.examples.resources.client.model.Post;
import com.sencha.gxt.examples.resources.client.model.PostProperties;
import com.sencha.gxt.explorer.client.app.ui.ExampleContainer;
import com.sencha.gxt.explorer.client.model.Example.Detail;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.box.ConfirmMessageBox;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.DialogHideEvent;
import com.sencha.gxt.widget.core.client.event.RefreshEvent;
import com.sencha.gxt.widget.core.client.form.CheckBox;
import com.sencha.gxt.widget.core.client.grid.CheckBoxSelectionModel;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.toolbar.PagingToolBar;

import static com.sencha.gxt.widget.core.client.Dialog.PredefinedButton.YES;

public class PagingGridExample implements IsWidget, EntryPoint {

  protected static final int MAX_HEIGHT = 600;
  protected static final int MAX_WIDTH = 800;
  protected static final int MIN_HEIGHT = 320;
  protected static final int MIN_WIDTH = 550;

  private ContentPanel panel;

  @Override
  public Widget asWidget() {
    if (panel == null) {
      final ExampleServiceAsync service = GWT.create(ExampleService.class);

      RpcProxy<PagingLoadConfig, PagingLoadResult<Post>> rpxProxy = new RpcProxy<PagingLoadConfig, PagingLoadResult<Post>>() {
        @Override
        public void load(PagingLoadConfig loadConfig, AsyncCallback<PagingLoadResult<Post>> callback) {
          service.getPosts(loadConfig, callback);
        }
      };

      PostProperties properties = GWT.create(PostProperties.class);

      ListStore<Post> store = new ListStore<Post>(new ModelKeyProvider<Post>() {
        @Override
        public String getKey(Post item) {
          return "" + item.getId();
        }
      });

      final PagingGridConfirmableLoader loader = new PagingGridConfirmableLoader(rpxProxy);
      loader.setRemoteSort(true);
      loader.addLoadHandler(new LoadResultListStoreBinding<PagingLoadConfig, Post, PagingLoadResult<Post>>(store));

      IdentityValueProvider<Post> identity = new IdentityValueProvider<Post>();
      final CheckBoxSelectionModel<Post> selectionModel = new CheckBoxSelectionModel<Post>(identity) {
        @Override
        protected void onRefresh(RefreshEvent event) {
          // this code selects all rows when paging if the header checkbox is selected
          if (isSelectAllChecked()) {
            selectAll();
          }
          super.onRefresh(event);
        }
      };

      ColumnConfig<Post, String> forumColumn = new ColumnConfig<Post, String>(properties.forum(), 130, "Forum");
      ColumnConfig<Post, String> usernameColumn = new ColumnConfig<Post, String>(properties.username(), 100, "Username");
      ColumnConfig<Post, String> subjectColumn = new ColumnConfig<Post, String>(properties.subject(), 150, "Subject");
      ColumnConfig<Post, Date> dateColumn = new ColumnConfig<Post, Date>(properties.date(), 100, "Date");

      dateColumn.setCell(new DateCell(DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT)));

      List<ColumnConfig<Post, ?>> columns = new ArrayList<ColumnConfig<Post, ?>>();
      // The selection model provides the first column config
      columns.add(selectionModel.getColumn());
      columns.add(forumColumn);
      columns.add(usernameColumn);
      columns.add(subjectColumn);
      columns.add(dateColumn);

      ColumnModel<Post> cm = new ColumnModel<Post>(columns);

      Grid<Post> grid = new Grid<Post>(store, cm) {
        @Override
        protected void onAfterFirstAttach() {
          super.onAfterFirstAttach();
          Scheduler.get().scheduleDeferred(new ScheduledCommand() {
            @Override
            public void execute() {
              loader.load();
            }
          });
        }
      };
      grid.setSelectionModel(selectionModel);
      grid.getView().setAutoExpandColumn(subjectColumn);
      grid.setLoadMask(true);
      grid.setLoader(loader);
      grid.setColumnReordering(true);

      final CheckBox warnLoad = new CheckBox();
      warnLoad.setBoxLabel("Warn before loading new data");
      warnLoad.setValue(false);

      final PagingToolBar toolBar = new PagingToolBar(50);
      toolBar.setBorders(false);
      toolBar.bind(loader);

      VerticalLayoutContainer verticalLayoutContainer = new VerticalLayoutContainer();
      verticalLayoutContainer.add(grid, new VerticalLayoutData(1, 1));
      verticalLayoutContainer.add(toolBar, new VerticalLayoutData(1, -1));
      verticalLayoutContainer.add(warnLoad, new VerticalLayoutData(-1, -1, new Margins(0, 0, 3, 3)));

      // If the warn checkbox is active, then present a warning and stop any load
      // after the first if the user clicks cancel
      loader.addBeforeLoadHandler(new BeforeLoadHandler<PagingLoadConfig>() {
        boolean initialLoad = true;

        @Override
        public void onBeforeLoad(BeforeLoadEvent<PagingLoadConfig> event) {
          if (!initialLoad && warnLoad.getValue()) {
            event.setCancelled(true);
            ConfirmMessageBox confirm = new ConfirmMessageBox("Confirm", "Are you sure you want to do that?");
            confirm.addDialogHideHandler(new DialogHideEvent.DialogHideHandler() {
              @Override
              public void onDialogHide(DialogHideEvent event) {
                if (event.getHideButton() == YES) {
                  loader.loadData(loader.getLastLoadConfig());
                }
              }
            });
            confirm.setWidth("300");
            confirm.show();
          }
          initialLoad = false;
        }
      });

      panel = new ContentPanel();
      panel.setHeading("Paging Grid");
      panel.add(verticalLayoutContainer);
    }

    return panel;
  }

  @Override
  public void onModuleLoad() {
    new ExampleContainer(this)
        .setMaxHeight(MAX_HEIGHT)
        .setMaxWidth(MAX_WIDTH)
        .setMinHeight(MIN_HEIGHT)
        .setMinWidth(MIN_WIDTH)
        .doStandalone();
  }

}
</textarea>

<script src="../../samples/ace/ace.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
  var type = "{type}";
  if (type == ("{" + "type}")) {
    // something went wrong, use java as default
    type = "java";
  }
  var editor = ace.edit("source");
  var mode = "ace/mode/" + type;
  var theme = "ace/theme/chrome";
  var readOnly = true;
  editor.getSession().setMode(mode);
  editor.getSession().setUseWorker(false);
  editor.setTheme(theme);
  editor.setShowPrintMargin(false);
  editor.renderer.setShowGutter(true);
  editor.setHighlightActiveLine(false);
  editor.setDisplayIndentGuides(true);
  editor.setReadOnly(readOnly);
  editor.renderer.setPadding(10);
  editor.renderer.setScrollMargin(10, 10, 0, 0);
  editor.getSession().setOptions({
    tabSize: 8,
    useSoftTabs: true,
    useWrapMode: false,
  });
</script>
</body>
</html>